package com.my.helper.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.my.helper.R;
import com.my.helper.util.CacheActivity;
import com.my.helper.util.MyDataBaseHelper;

public class BeiWangActivity extends AppCompatActivity implements View.OnClickListener {
    private RelativeLayout rlBeiWang;
    private TextView tvContent;
    private TextView tvTime;
    private TextView tvDelete;
    private String TAG = "mwb";
    private MyDataBaseHelper mSql;
    private String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bei_wang);
        CacheActivity.addActivity(this);

        mSql = new MyDataBaseHelper(this, "Helper.db", null, 1);
        Intent intent = getIntent();
        tvContent = findViewById(R.id.tv_content_bw);
        tvTime = findViewById(R.id.tv_time_bw);

        content = intent.getStringExtra("content");
        tvContent.setText(intent.getStringExtra("content"));
        tvTime.setText(intent.getStringExtra("time"));

        rlBeiWang = findViewById(R.id.rl_back_bw);
        rlBeiWang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CacheActivity.finishSingleActivityByClass(BeiWangActivity.class);
            }
        });

        tvDelete = findViewById(R.id.tv_delete_message_web);
        tvDelete.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_delete_message_web: // 删除
                goDelete();
                break;
        }
    }

    private void goDelete() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        builder.setTitle("问题：");
        builder.setMessage("是否要删除此备忘录");
//        builder.setIcon(R.mipmap.ic_launcher_round);
        //点击对话框以外的区域是否让对话框消失
        builder.setCancelable(true);
        //设置正面按钮
        builder.setPositiveButton("是的", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                deleteData();
                dialog.dismiss();
            }
        });
        //设置反面按钮
        builder.setNegativeButton("不是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setNeutralButton("", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                Toast.makeText(BeiWangActivity.this, "你选择了保密", Toast.LENGTH_SHORT).show();
//                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        //对话框显示的监听事件
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Log.e(TAG, "对话框显示了");
            }
        });
        //对话框消失的监听事件
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                Log.e(TAG, "对话框消失了");
            }
        });
        //显示对话框
        dialog.show();
    }

    /**
     * 删除数据
     */
    private void deleteData() {
        SQLiteDatabase db = mSql.getWritableDatabase();
        db.delete("MyMemorandum", "content=?", new String[]{content});
        db.close();
        CacheActivity.finishSingleActivityByClass(BeiWangActivity.class);
    }
}
